﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DATA.BusinessObjects;
using DATA.ManagerObjects;
using BUS;

namespace Presentation
{
    public partial class frmTiepNhanPhong : Form
    {
        private int intStt = 1;
        public frmTiepNhanPhong()
        {
            InitializeComponent();
        }

        private void LoadDanhSachPhong()
        {
            try
            {
                dgv_DanhSachPhong.Rows.Clear();
                // Load danh sach phong vao datagridview
                IList<Phong> lstPhong = PhongBUS.LayDanhSachPhong();
                if (lstPhong != null)
                {
                    int istt = 1;
                    for (int i = 0; i < lstPhong.Count; i++)
                    {
                        Phong phong = lstPhong[i];
                        string[] row = new string[] { istt++.ToString(), phong.Id, phong.TenPhong, phong.Loaiphong.TenLoaiPhong, phong.TinhTrangPhong.ToString(), phong.GhiChu };
                        dgv_DanhSachPhong.Rows.Add(row);
                    }
                }

                // Load danh sach loai phong vao combobox
                DataGridViewComboBoxColumn dgvcbocolumn = (DataGridViewComboBoxColumn)dgv_PhongTiepNhan.Columns["dgvTenLoaiPhong"];
                IList<Loaiphong> lstLoaiPhong = LoaiPhongBUS.LayDanhSachLoaiPhong();
                if (lstLoaiPhong != null)
                {
                    dgvcbocolumn.DataSource = lstLoaiPhong;
                    dgvcbocolumn.DisplayMember = "TenLoaiPhong";
                    dgvcbocolumn.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi ở form load", ex);
            }
        }

        private void btn_ThemPhong_Click(object sender, EventArgs e)
        {
            int iRows = dgv_PhongTiepNhan.Rows.Count;
            if (iRows > 0)
            {
                for (int i = 0; i <iRows-1; i++)
                {
                    Phong phongMoi = new Phong();
                    String tenPhong = "";
                    String tenLoaiPhong = "MLP1";
                    String ghiChu = "";
                    if (dgv_PhongTiepNhan.Rows[i].Cells["dgvTenPhong"].Value != null)
                    {
                        tenPhong = dgv_PhongTiepNhan.Rows[i].Cells["dgvTenPhong"].Value.ToString();
                    }
                    if (dgv_PhongTiepNhan.Rows[i].Cells["dgvTenLoaiPhong"].Value != null)
                    {
                        tenLoaiPhong = dgv_PhongTiepNhan.Rows[i].Cells["dgvTenLoaiPhong"].Value.ToString();
                    }
                    if (dgv_PhongTiepNhan.Rows[i].Cells["dgvGhiChu"].Value != null)
                    {
                        ghiChu = dgv_PhongTiepNhan.Rows[i].Cells["dgvGhiChu"].Value.ToString();
                    }
                    Phong phongCuoiCung = PhongBUS.LayPhongCuoiCung();
                    String maPhongMoi = "";
                    if (phongCuoiCung != null)
                    {
                        maPhongMoi = ThamSoBUS.TaoMaTangTuDong(phongCuoiCung.Id, 2, "MP");
                    }
                    phongMoi.Id = maPhongMoi;
                    phongMoi.TenPhong = tenPhong;
                    phongMoi.GhiChu = ghiChu;
                    phongMoi.TinhTrangPhong = 0;    // 0: còn trống | 1: có người thuê

                    Loaiphong loaiPhong = LoaiPhongBUS.LayLoaiPhongTheoMaLoaiPhong(tenLoaiPhong);
                    if (loaiPhong != null)
                    {
                        phongMoi.Loaiphong = loaiPhong;
                    }
                    
                    bool flag = PhongBUS.ThemPhong(phongMoi);
                    if (flag == false)
                    {
                        MessageBox.Show("Không thêm được phòng: "+i.ToString());
                    }
                }
                MessageBox.Show("Thêm thành công phòng");
                dgv_PhongTiepNhan.Rows.Clear();
                LoadDanhSachPhong();
            }
        }

        private void frmTiepNhanPhong_Load(object sender, EventArgs e)
        {
            //skinFrmTiepNhanPhong.ApplyAdditionalBuiltInSkins(0);
            //skinEngine1.ApplyAdditionalBuiltInSkins(0);
            try
            {
                LoadDanhSachPhong();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi ở form load", ex);
            }
        }

        private void dgv_PhongTiepNhan_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dgvRow = dgv_PhongTiepNhan.CurrentRow;
            dgvRow.Cells["dgvSTT"].Value = intStt;
            intStt++;
            string strMaLoaiPhong = "";
            string strGia = "";
            if (dgvRow.Cells["dgvTenLoaiPhong"].Value != null)
            {
                strMaLoaiPhong = dgvRow.Cells["dgvTenLoaiPhong"].Value.ToString();
            }
            Loaiphong loaiPhong = LoaiPhongBUS.LayLoaiPhongTheoMaLoaiPhong(strMaLoaiPhong);
            float donGia = 0;
            if (loaiPhong != null)
            {
                donGia = (float)loaiPhong.DonGia;
                strGia = donGia.ToString();
                dgvRow.Cells["dgvDonGia"].Value = strGia;
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_TimPhong_Click(object sender, EventArgs e)
        {
            frmTraCuuPhong frm = new frmTraCuuPhong();
            frm.Show();
        }

        private void btn_LamMoi_Click(object sender, EventArgs e)
        {
            dgv_PhongTiepNhan.Rows.Clear();
        }

        private void dgv_PhongTiepNhan_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show("chim");
        }

        private void dgv_PhongTiepNhan_CurrentCellChanged(object sender, EventArgs e)
        {
            //MessageBox.Show("chim");
        }
    }
}
